import { createRouter, createWebHashHistory } from "vue-router";
import UserLogin from "@/components/UserLogin.vue";
import mainpage from "@/components/frame/mainpage.vue";
import { useUserStore } from "../store/userStore.js";

const routes = [
    {
        path: '/login',
        component: UserLogin
    },
    {
        name: "mainpage",
        path: '/',
        component: mainpage,
        children: [
            {
                name: 'checkIn',
                path: '/customer/checkIn',
                component: function () {
                    return import('@/views/CustomerManage/CheckIn.vue')
                }
            },
            {
                name: 'checkOut',
                path: '/customer/checkOut',
                component: function () {
                    return import('@/views/CustomerManage/CheckOut.vue')
                }
            },
            {
                name: 'outRecords',
                path: '/customer/outRecords',
                component: function () {
                    return import('@/views/CustomerManage/OutRecords.vue')
                }
            },
            {
                name: 'userManagement',
                path: '/system/userManagement',
                component: function () {
                    return import('@/views/UserManage/userManagement.vue')
                }
            },
            {
                name: 'bedManage',
                path: '/bed/bedManage',
                component: function () {
                    return import('@/views/BedManage/bedManage.vue')
                }
            },
            {
                name: 'bedMap',
                path: '/bed/bedMap',
                component: function () {
                    return import('@/views/BedManage/bedMap.vue')
                }
            },
            {
                name: 'mealManage',
                path: '/customer/mealManage',
                component: function () {
                    return import('@/views/MealManage/mealManage.vue')
                }
            },
            {
                name: 'mealCal',
                path: '/customer/mealCal',
                component: function () {
                    return import('@/views/MealManage/mealCal.vue')
                }
            },
            {
                name: 'nurseItem',
                path: '/nurse/nurseItem',
                component: function () {
                    return import('@/views/nurse/nurseItem/index.vue')
                }
            },
            {
                name: 'nurseLevel',
                path: '/nurse/nurseLevel',
                component: function () {
                    return import('@/views/nurse/nurseLevel/index.vue')
                }
            },
            {
                name: 'nurseSetting',
                path: '/nurse/nurseSetting',
                component: function () {
                    return import('@/views/nurse/nurseSetting/index.vue')
                }
            },
            {
                name: 'nurseRecords',
                path: '/nurse/nurseRecords',
                component: function () {
                    return import('@/views/nurse/nurseRecords/index.vue')
                }
            },
            {
                name: 'serviceCare',
                path: '/health/serviceCare',
                component: function () {
                    return import('@/views/health/serviceCare/index.vue')
                }
            },
            {
                name: 'serviceCustomerSetting',
                path: '/health/serviceCustomerSetting',
                component: function () {
                    return import('@/views/health/serviceCustomerSetting/index.vue')
                }
            },
            {
                name: 'EditPim',
                path: '/EditPim',
                component: function () {
                    return import('@/views/Pim/EditPim.vue')
                }
            }
        ]
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

const whiteList = ['/login']
router.beforeEach(
    (to, from, next) => {
        const path = to.path
        const userStore = useUserStore();

        if (whiteList.includes(path)) {
            console.log("路由守卫：白名单直接跳转");
            next();
            return true;
        }

        if (localStorage.getItem('token')) {
            console.log("路由守卫：通过");
            next();
            return true;
        } else {
            console.log("路由守卫：未登录");
            next("/login")
            return false;
        }
    }
)

export default router;